﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dack_Web2.DTO;
using Dack_Web2.BUS;
namespace Dack_Web2
{
    public partial class DangSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sql = "select * from LoaiSanPham ";
                drLoaiSanPham.DataSource = Sql_DataProvider.SQL_ExecuteTable(sql);
                drLoaiSanPham.DataValueField = "MaLoaiSanPham";
                drLoaiSanPham.DataTextField = "TenLoaiSanPham";
                drLoaiSanPham.DataBind();
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
                try
                {
                    string UpLoadFolder = Server.MapPath("~/Images/ImageUpload");
                    if (!Directory.Exists(UpLoadFolder))
                        Directory.CreateDirectory(UpLoadFolder);
                    HttpFileCollection fc = Request.Files;

                    for (int i = 0; i < fc.Count; i++)
                    {
                        HttpPostedFile pf = fc[i];
                        if (pf.ContentLength > 0)
                        {
                            string filename = Path.Combine(UpLoadFolder,DateTime.Now.ToString("dd_H_mm_ss") + pf.FileName);
                            pf.SaveAs(filename);
                            filename = filename.Replace(@"\", @"/");
                            lbUploadCmp.Text += "<br> Link "+ i.ToString() +" : " + filename;
                        }
                    }
                }
                catch (Exception ex) { Response.Write(ex.Message); }
           
        }

        protected void btLuuSP_Click(object sender, EventArgs e)
        {
            SanPhamDTO SanPham = new SanPhamDTO();
            SanPham.GiaSanPham = Convert.ToInt32(txtGia.Text);
            SanPham.HinhGioiThieu = txtHinhGioiThieu.Text;
            SanPham.MaUser = Dack_Web2.User.FromId(CurrentContext.UserName).MaUser;
            SanPham.NgayDang = DateTime.Now.Date;
            SanPham.MaLoaiSanPham = Convert.ToInt32(drLoaiSanPham.SelectedValue.ToString());
            SanPham.TenSanPham = txtTenSanPham.Text;
            SanPham.ThongTinChiTiet = txtThongTinChiTiet.Text;
            SanPhamBUS.Insert(SanPham);
            lbThongBao.Text = "Bạn đã thêm sản phẩm thành công !!! Bạn có thể mở Deal hoặc thêm 1 sản phẩm khác ";
        }


        
    }
}